package com.diandian.backend.jingdong.home.exchange.controller;

import com.alibaba.fastjson.JSONObject;
import com.diandian.backend.jingdong.home.exchange.service.BusinessJingdongTokenService;
import com.diandian.base.boot.starter.response.BaseResult;
import com.diandian.base.boot.starter.util.ResultUtil;
import com.diandian.jingdong.home.boot.starter.service.token.dto.TokenRequestDTO;
import com.diandian.jingdong.home.boot.starter.service.token.dto.TokenResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/business/token")
public class BusinessTokenController {

    @Autowired
    private BusinessJingdongTokenService businessJingdongTokenService;

    /**
     * 接收京东官方调用，传入新的token
     * @param appKey
     * @param appSecret
     * @param token
     * @return
     * @throws Exception
     */
    @PostMapping("/accept")
    public TokenResponseDTO acceptTokenInfo(@RequestParam(name = "appKey") String appKey,
                                            @RequestParam(name = "appSecret") String appSecret,
                                            @RequestParam(name = "token") String token) throws Exception{
        TokenRequestDTO tokenResponseDTO = JSONObject.parseObject(token, TokenRequestDTO.class);
        return businessJingdongTokenService.acceptTokenInfo(tokenResponseDTO, appKey, appSecret);
    }

    /**
     * 根据appKey获取对应的token
     * @param appKey
     * @return
     */
    @GetMapping("/getToken")
    public BaseResult getToken(@RequestHeader(name = "appKey") String appKey) {
        return ResultUtil.makeResult(businessJingdongTokenService.getToken(appKey));
    }

}
